package com.base.context.event;

import java.lang.annotation.Annotation;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
/**
 * 用于spring容器实例化gua事件监听器后注册监听器
 * @author Jason
 *
 */
@Component
// 所有实现了BeanPostProcessor的处理器都会在springIOC创建bean时自动调用
public class ListenerRegister implements BeanPostProcessor {

	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		return bean;
	}

	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		Annotation[] annos = bean.getClass().getAnnotations();
		for(Annotation anno:annos) {
			if (anno.annotationType().equals(Listener.class)){
				EventUtils.register(bean);
			}
		}
		return bean;
	}

}
